<?php

/**
 * This is the model class for table "request_konsumen".
 *
 * The followings are the available columns in table 'request_konsumen':
 * @property integer $id
 * @property integer $rapat_id
 * @property string $message
 * @property string $status
 *
 * The followings are the available model relations:
 * @property MakananKonsumen[] $makananKonsumens
 * @property Rapat $rapat
 */
class RequestKonsumen extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return RequestKonsumen the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'request_konsumen';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('rapat_id', 'required'),
			array('rapat_id', 'numerical', 'integerOnly'=>true),
			array('status', 'length', 'max'=>8),
			array('message', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, rapat_id, message, status,message_admin', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'makananKonsumens' => array(self::HAS_MANY, 'MakananKonsumen', 'request_id'),
			'rapat' => array(self::BELONGS_TO, 'Rapat', 'rapat_id'),
		//	'makananKonsumenSumNasiBox' => array(self::STAT,'MakananKonsumen','request_id',
			//	'select' => 'SUM(quantity)',
			//	'condition' => array('')
		);
		
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'rapat_id' => 'Rapat',
			'message' => 'Pesan',
			'status' => 'Status',
			'message_admin' => 'Pesan dari Admin'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('rapat_id',$this->rapat_id);
		$criteria->compare('message',$this->message,true);
		$criteria->compare('status',$this->status,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public function getJumlahDiperlukan($angka){
	
	$query= "SELECT SUM(quantity) FROM `makanan_konsumen`,`request_konsumen` WHERE `type_id` =".$angka." AND `request_konsumen`.`id`=`makanan_konsumen`.`request_id` AND `request_konsumen`.`status`= 2";
	$command = Yii::app()->db->createCommand($query);
	
	$result= $command->query();
	$row = $result->read();
	if ($row['SUM(quantity)'] != NULL){
	return $row['SUM(quantity)'];}
	else{
		$row['SUM(quantity)'] = 0;
		return $row['SUM(quantity)'];
	}

	
	//$row = Yii::app()->db->createCommand(array(
    //'select' => array('quantity'),
    //'from' => array('makanan_konsumen.type_id p','request_konsumen'),
    //'where' => array('p=:id','request_konsumen.id=makanan_konsumen.request_id','request_konsumen.status=2'),
    //'params' => array(':id'=>$type),
	//))->queryRow();

		
//	$user = Yii::app()->db->createCommand()
  //  ->select('quantity')
    //->from('makanan_konsumen mk')
    //->leftJoin('request_konsumen kp', 'mk.request_id=kp.id')
    //->where('kp.status=2') 
    //->query();
	//return 'mama';
	//return $user;
	
	}
	
}